'use strict'
const path = require('path')
const defaultSettings = require('./src/settings.js')
function resolve(dir) {
  return path.join(__dirname, dir)
}

const name = defaultSettings.title || '技能强国后台管理系统' // page title
const port = 9526 // dev port
const env = 'test'
module.exports = {
  // 部署生产环境和开发环境下的URL：可对当前环境进行区分，baseUrl 从 Vue CLI 3.3 起已弃用，要使用publicPath
  publicPath: './',
  // 输出文件目录：在npm run build时，生成文件的目录名称
  outputDir: 'admin',
  // 放置生成的静态资源 (js、css、img、fonts) 的 (相对于 outputDir 的) 目录
  assetsDir: 'admin',
  // 代码保存时进行eslint检测
  lintOnSave: false,
    // 是否在构建生产包时生成 sourceMap 文件，false将提高构建速度   映射文件 打包时使用
  productionSourceMap: false,
/*  // 热更新
  hotOnly: false,*/
  // webpack-dev-server 相关配置
  devServer: {
    //端口
    port: port,
    // 自动打开浏览器
    open: true,
    overlay: {
      warnings: false,
      errors: true
    },
    //使用代理
    proxy: {
      '/API-ENTERPRISE-APP/': {
        // 目标代理服务器地址
        target: env === 'test' ? 'http://cxhg-qy01-test.bjttsx.com/API-ENTERPRISE-APP/' : 'http://192.168.127.224:14001',
        // target: env === 'test' ? 'http://cxhg-qy01-test.bjttsx.com/API-ENTERPRISE-APP/' : 'http://192.168.127.224:14001',
        // 开启代理，本地创建一个虚拟服务器 允许跨域
        changeOrigin: true,
        pathRewrite: {
          '^/API-ENTERPRISE-APP/': '/'
        }
      },
      '/API-ENTERPRISE-BACKEND/': {
        target: env === 'test' ? 'http://cxhg-qy01-test.bjttsx.com/API-ENTERPRISE-BACKEND/' : 'http://192.168.127.224:14000',
        changeOrigin: true,
        pathRewrite: {
          '^/API-ENTERPRISE-BACKEND/': '/'
        }
      },
      '/API-DOCUMENT/': {
        target: env === 'test' ? 'http://cxhgtest.bjttsx.com/API-DOCUMENT/' : 'http://cxhgtest.bjttsx.com/API-DOCUMENT/',
        changeOrigin: true,
        pathRewrite: {
          '^/API-DOCUMENT/': '/'
        }
      },
      '/API-LEARN/': {
        target: env === 'test' ? 'http://cxhg-qy01-test.bjttsx.com/API-ENTERPRISE-LEARN/' : 'http://192.168.127.224:14009',
        changeOrigin: true,
        pathRewrite: {
          '^/API-ENTERPRISE-LEARN/': '/'
        }
      },
      '/API-ENTERPRISE-NEWS/': {
        target: env === 'test' ? 'http://cxhg-qy01-test.bjttsx.com/API-ENTERPRISE-NEWS/' : 'http://192.168.127.224:14010',
        changeOrigin: true,
        pathRewrite: {
          '^/API-ENTERPRISE-NEWS/': '/'
        }
      },
      '/API-ENTERPRISE-LEARN/': {
        target: env === 'test' ? 'http://cxhg-qy01-test.bjttsx.com/API-ENTERPRISE-LEARN/' : 'http://192.168.127.224:14009',
        changeOrigin: true,
        pathRewrite: {
          '^/API-ENTERPRISE-LEARN/': '/'
        }
      },

      '/API-ENTERPRISE-ACTIVITY/': {
        target: env === 'test' ? 'http://cxhg-qy01-test.bjttsx.com/API-ENTERPRISE-ACTIVITY/' : env === '本地外网' ? 'http://103.91.179.38:14008' : 'http://192.168.127.224:14008',
        changeOrigin: true,
        pathRewrite: {
          '^/API-ENTERPRISE-ACTIVITY/': '/'
        }
      },
      '/API-ENTERPRISE-CUSTOM/': {
        target: env === 'test' ? 'http://cxhg-qy01-test.bjttsx.com/API-ENTERPRISE-CUSTOM/' : env === '本地外网' ? 'http://103.91.179.38:14012' : 'http://192.168.127.224:14012',
        changeOrigin: true,
        pathRewrite: {
          '^/API-ENTERPRISE-CUSTOM/': '/'
        }
      },
      '/API-ENTERPRISE-WELFARE/': {
        target: env === 'test' ? 'http://cxhg-qy01-test.bjttsx.com/API-ENTERPRISE-WELFARE/' : env === '本地外网' ? 'http://103.91.179.38:14011' : 'http://192.168.127.224:14011',
        changeOrigin: true,
        pathRewrite: {
          '^/API-ENTERPRISE-WELFARE/': '/'
        }
      },
    }
  },
  configureWebpack: {
    // provide the app's title in webpack's name field, so that
    // it can be accessed in index.html to inject the correct title.
    name: name,
    resolve: {
      alias: {
        '@': resolve('src')
      }
    }
  },
  css: {
    loaderOptions: {
      sass: {
        // 根据自己样式文件的位置调整
        data: '@import "@/styles/mixin.scss";'
      }
    }
  },
  chainWebpack(config) {
    config.plugins.delete('preload') // TODO: need test
    config.plugins.delete('prefetch') // TODO: need test
    //config.plugin('webpack-bundle-analyzer').use(require('webpack-bundle-analyzer').BundleAnalyzerPlugin) //项目分析
    // set svg-sprite-loader
    // set svg-sprite-loader
    config.module
      .rule('svg')
      .exclude.add(resolve('src/icons/svg'))
      .end()
    config.module
      .rule('svgs')
      .test(/\.svg$/)
      .include.add(resolve('src/icons/svg'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
      .end()

    config.module
      .rule('icon')
      .exclude.add(resolve('src/icons/icon'))
      .end()
    config.module
      .rule('icons')
      .test(/\.icon$/)
      .include.add(resolve('src/icons/icon'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
      .end()
    // set preserveWhitespace
    config.module
      .rule('vue')
      .use('vue-loader')
      .loader('vue-loader')
      .tap(options => {
        options.compilerOptions.preserveWhitespace = true
        return options
      })
      .end()

    config
      // https://webpack.js.org/configuration/devtool/#development
      .when(process.env.NODE_ENV === 'development',
        config => config.devtool('cheap-source-map')
      )

    config
      .when(process.env.NODE_ENV !== 'development',
        config => {
          config
            .plugin('ScriptExtHtmlWebpackPlugin')
            .after('html')
            .use('script-ext-html-webpack-plugin', [{
              // `runtime` must same as runtimeChunk name. default is `runtime`
              inline: /runtime\..*\.js$/
            }])
            .end()
          config
            .optimization.splitChunks({
            chunks: 'all',
            maxSize: 1048960,
            minSize: 511600,
            cacheGroups: {
              libs: {
                name: 'chunk-libs',
                test: /[\\/]node_modules[\\/]/,
                priority: 10,
                chunks: 'initial' // only package third parties that are initially dependent
              },
              elementUI: {
                name: 'chunk-elementUI', // split elementUI into a single package
                priority: 3, // the weight needs to be larger than libs and app or it will be packaged into libs or app
                test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
              },
              commons: {
                name: 'chunk-commons',
                test: resolve('src/components'), // can customize your rules
                minChunks: 3, //  minimum common number
                priority: 5,
                reuseExistingChunk: true
              }
            }
          })
          config.optimization.runtimeChunk('single')
        }
      )
  }
}
